<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="养殖户" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入养殖户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="保单号" prop="policyNumber">
              <el-input
                v-model="queryParams.policyNumber"
                placeholder="请输入保单号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="投保人姓名" prop="insureName">
              <el-input
                v-model="queryParams.insureName"
                placeholder="请输入投保人姓名"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="投保人手机号" prop="insurePhone" label-width="100px">
        <el-input
          v-model="queryParams.insurePhone"
          placeholder="请输入投保人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受益人姓名" prop="beneficiaryName" label-width="100px">
        <el-input
          v-model="queryParams.beneficiaryName"
          placeholder="请输入受益人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="险种名称" prop="insuranceType">
        <el-select v-model="queryParams.insuranceType" placeholder="请选择险种名称" clearable size="small">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dgw:insurance:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dgw:insurance:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dgw:insurance:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dgw:insurance:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="insuranceList.slice((pageNum-1)*pageSize,pageNum*pageSize)"  @selection-change="handleSelectionChange">

      <el-table-column toggleRowExpansion="row" fixed type="expand">
        <template slot-scope="scope">
          <el-table :data="scope.row.children"
                    height="400px"
                    width="50%"
                    :row-class-name="tableRowClassName">
            <el-table-column label="耳标编号" align="center" prop="earNumber" width="300"/>
            <el-table-column label="温度" align="center" prop="temp" width="80">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.temp< 20?'danger': 27<=scope.row.temp <=38?'primary':'danger'">
                  {{scope.row.temp}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="计步" align="center" prop="jbu" width="80"/>
            <el-table-column label="电量" align="center" prop="power" width="80">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.power >=70?'primary': 50<scope.row.power <70?'warning':'danger'">
                  {{scope.row.power}}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column label="用户手机号" align="center" prop="phone" width="120" />
            <el-table-column label="牛品种" align="center" prop="cowType"width="80">
              <template slot-scope="scope">
                <span>{{scope.row.cowType==1?'育肥牛':scope.row.cowType==2?'牛犊':scope.row.cowType==3?'奶牛':'母牛'}}</span>
              </template>
            </el-table-column>
            <el-table-column label="牛棚名称" align="center" prop="cowShedName" width="120"/>
            <el-table-column label="牛棚地址" align="center" prop="shedAddress" width="300"/>
          </el-table>
        </template>
      </el-table-column>

      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column fixed label="养殖户" align="center" prop="nickName"/>
      <el-table-column label="投保单号" align="center" prop="insuranceNumber" width="150px"/>
      <el-table-column label="保单号" align="center" prop="policyNumber" width="150px"/>
      <el-table-column label="报案号" align="center" prop="reportNumber" width="150px"/>
      <el-table-column label="备案号" align="center" prop="recordsNumber" width="150px"/>
      <el-table-column label="投保人姓名" align="center" prop="insureName" width="90px"/>
      <el-table-column label="投保人身份证" align="center" prop="insureSfz" width="200"/>
      <el-table-column label="投保人银行卡号" align="center" prop="insureYhkh" width="200"/>
      <el-table-column label="投保人手机号" align="center" prop="insurePhone" width="100px"/>
      <el-table-column label="受益人姓名" align="center" prop="beneficiaryName" width="90px"/>
      <el-table-column label="受益人身份证" align="center" prop="beneficiarySfz" width="150"/>
      <el-table-column label="受益人银行卡号" align="center" prop="beneficiaryYhkh" width="200"/>
      <el-table-column label="险种名称" align="center" prop="insuranceType" width="90px"/>
      <el-table-column label="所属区域" align="center" prop="area" width="150px"/>
      <el-table-column label="承保头数" align="center" prop="insuranceSum" width="80"/>
      <el-table-column label="生效时间" align="center" prop="effectiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="deadline" width="180">
        <template slot-scope="scope">
          <span>{{


              parseTime(scope.row.deadline, '{y}-{m}-{d} {h}:{m}:{s}')
            }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="审核状态" align="center" prop="status" >
              <template slot-scope="scope">
                <el-tag :type="scope.row.status==1?'info':scope.row.status==2?'primary':'danger'">
                  {{scope.row.status==1?'未审核':scope.row.status==2?'审核通过':'驳回'}}
                </el-tag>
              </template>
            </el-table-column>-->
      <!--      <el-table-column label="驳回原因" align="center" prop="cause" />-->

      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleClick(scope.row)"
          >查询原因
          </el-button>
          <!-- @click="handleUpdate(scope.row)"
                            v-hasPermi="['dgw:insurance:edit']"               -->
          <!--                <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['dgw:insurance:remove']"
                          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      :page-sizes="[15, 20, 30, 40, 50]"
      @pagination="getList"
    />

    <!-- 添加或修改保险对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="投保单号" prop="insuranceNumber">
          <el-input v-model="form.insuranceNumber" placeholder="请输入投保单号"/>
        </el-form-item>
        <el-form-item label="保单号" prop="policyNumber">
          <el-input v-model="form.policyNumber" placeholder="请输入保单号"/>
        </el-form-item>
        <el-form-item label="报案号" prop="reportNumber">
          <el-input v-model="form.reportNumber" placeholder="请输入报案号"/>
        </el-form-item>
        <el-form-item label="备案号" prop="recordsNumber">
          <el-input v-model="form.recordsNumber" placeholder="请输入备案号"/>
        </el-form-item>
        <el-form-item label="投保人姓名" prop="insureName">
          <el-input v-model="form.insureName" placeholder="请输入投保人姓名"/>
        </el-form-item>
        <el-form-item label="投保人身份证" prop="insureSfz">
          <el-input v-model="form.insureSfz" placeholder="请输入投保人身份证"/>
        </el-form-item>
        <el-form-item label="投保人银行卡号" prop="insureYhkh">
          <el-input v-model="form.insureYhkh" placeholder="请输入投保人银行卡号"/>
        </el-form-item>
        <el-form-item label="投保人手机号" prop="insurePhone">
          <el-input v-model="form.insurePhone" placeholder="请输入投保人手机号"/>
        </el-form-item>
        <el-form-item label="受益人姓名" prop="beneficiaryName">
          <el-input v-model="form.beneficiaryName" placeholder="请输入受益人姓名"/>
        </el-form-item>
        <el-form-item label="受益人_身份证" prop="beneficiarySfz">
          <el-input v-model="form.beneficiarySfz" placeholder="请输入受益人_身份证"/>
        </el-form-item>
        <el-form-item label="受益人银行卡号" prop="beneficiaryYhkh">
          <el-input v-model="form.beneficiaryYhkh" placeholder="请输入受益人银行卡号"/>
        </el-form-item>
        <el-form-item label="险种名称" prop="insuranceType">
          <el-select v-model="form.insuranceType" placeholder="请选择险种名称">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域" prop="area">
          <el-input v-model="form.area" placeholder="请输入所属区域"/>
        </el-form-item>
        <el-form-item label="承保头数" prop="insuranceSum">
          <el-input v-model="form.insuranceSum" placeholder="请输入承保头数"/>
        </el-form-item>
        <el-form-item label="生效时间" prop="effectiveTime">
          <el-date-picker clearable size="small"
                          v-model="form.effectiveTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择生效时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="deadline">
          <el-date-picker clearable size="small"
                          v-model="form.deadline"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择截止时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态1=未审核，2=审核通过，3=驳回">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驳回原因" prop="cause">
          <el-input v-model="form.cause" placeholder="请输入驳回原因"/>
        </el-form-item>
        <el-form-item label="养殖户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入养殖户id"/>
        </el-form-item>
        <el-divider content-position="center">耳标信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTagNumber">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTagNumber">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="children" :row-class-name="rowTagNumberIndex"
                  @selection-change="handleTagNumberSelectionChange" ref="tagNumber"
        >
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="imei" prop="imei">
            <template slot-scope="scope">
              <el-input v-model="scope.row.imei" placeholder="请输入imei"/>
            </template>
          </el-table-column>
          <el-table-column label="耳标编号" prop="earNumber">
            <template slot-scope="scope">
              <el-input v-model="scope.row.earNumber" placeholder="请输入耳标编号"/>
            </template>
          </el-table-column>
          <el-table-column label="用户ID" prop="userId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入用户ID"/>
            </template>
          </el-table-column>
          <el-table-column label="用户手机号码" prop="phone">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phone" placeholder="请输入用户手机号码"/>
            </template>
          </el-table-column>
          <el-table-column label="该字段用于标识这个耳标是否使用.0是未使用 1是已使用" prop="isUse">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isUse" placeholder="请输入该字段用于标识这个耳标是否使用.0是未使用 1是已使用"/>
            </template>
          </el-table-column>
          <el-table-column label="养殖企业ID" prop="enterpriseId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.enterpriseId" placeholder="请输入养殖企业ID"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInsurance,
  getInsurance,
  delInsurance,
  addInsurance,
  updateInsurance,
  exportInsurance,
  treeListInsurance3
} from '@/api/dgw/insurance'

export default {
  name: 'Insurance',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTagNumber: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险表格数据
      insuranceList: [],
      // 耳标表格数据
      children: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      pageNum: 1,
      pageSize: 15,
      // 查询参数
      queryParams: {

        insuranceNumber: null,
        policyNumber: null,
        insureName: null,
        insurePhone: null,
        beneficiaryName: null,
        insuranceType: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        insuranceNumber: [
          { required: true, message: '投保单号不能为空', trigger: 'blur' }
        ],
        policyNumber: [
          { required: true, message: '保单号不能为空', trigger: 'blur' }
        ],
        reportNumber: [
          { required: true, message: '报案号不能为空', trigger: 'blur' }
        ],
        recordsNumber: [
          { required: true, message: '备案号不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '审核状态1=未审核，2=审核通过，3=驳回不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    /*表格颜色*/
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex != -1) {
        return 'success-row'
      }
      return ''
    },
    /** 查询保险列表 */
    getList() {
      this.loading = true
      treeListInsurance3(this.queryParams).then(response => {
        this.insuranceList = response.rows
        this.children = response.rows
        console.log('查询保险列表', this.children)
        console.log('列表', response)

        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insuranceNumber: null,
        policyNumber: null,
        reportNumber: null,
        recordsNumber: null,
        insureName: null,
        insureSfz: null,
        insureYhkh: null,
        insurePhone: null,
        beneficiaryName: null,
        beneficiarySfz: null,
        beneficiaryYhkh: null,
        insuranceType: null,
        area: null,
        insuranceSum: null,
        effectiveTime: null,
        deadline: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: 0,
        cause: null,
        userId: null,
        nickName: null
      }
      this.children = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加保险'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInsurance(id).then(response => {
        this.form = response.data
        this.children = response.data.children
        this.open = true
        this.title = '修改保险'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.children = this.children
          if (this.form.id != null) {
            updateInsurance(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInsurance(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除保险编号为"' + ids + '"的数据项？').then(function() {
        return delInsurance(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 耳标序号 */
    rowTagNumberIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 耳标添加按钮操作 */
    handleAddTagNumber() {
      let obj = {}
      obj.imei = ''
      obj.earNumber = ''
      obj.userId = ''
      obj.phone = ''
      obj.isUse = ''
      obj.enterpriseId = ''
      this.children.push(obj)
    },
    /** 耳标删除按钮操作 */
    handleDeleteTagNumber() {
      if (this.checkedTagNumber.length == 0) {
        this.$modal.msgError('请先选择要删除的耳标数据')
      } else {
        const children = this.children
        const checkedTagNumber = this.checkedTagNumber
        this.children = children.filter(function(item) {
          return checkedTagNumber.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleTagNumberSelectionChange(selection) {
      this.checkedTagNumber = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有保险数据项？').then(() => {
        this.exportLoading = true
        return exportInsurance(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    /*查看原因弹出层消息*/
    handleClick(row) {
      this.$alert(row.cause, '原因', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: '请通知用户重新提交申请'
          });
        }
      });
    }
  }
}
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
